package yourobot.bonus;

import java.awt.Color;
import java.util.Random;

import org.jbox2d.callbacks.QueryCallback;
import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.World;

import yourobot.robots.Player;
/**
 * 
 * @author mehdimasrour
 *
 */
public class Snap implements Bonus {

	private static final int MAX_X = 750;
	private static final int MAX_Y = 550;
	
	private final int x;
	private final int y;
	
	public Snap(){
		Random r = new Random(System.nanoTime());
		x = r.nextInt(MAX_X);
		y = r.nextInt(MAX_Y);
	}
	
	@Override
	public void useBonus(World w, final Player p) {
		AABB aabb = new AABB(new Vec2(p.getPosition().x-75, p.getPosition().y-75), new Vec2(p.getPosition().x+75, p.getPosition().y+75));
		w.queryAABB(new QueryCallback() {
			
			@Override
			public boolean reportFixture(Fixture fixture) {
				if ( fixture.getUserData().equals("wall1") ){
					double angle = Math.atan2(fixture.getBody().getPosition().y-p.getPosition().y, fixture.getBody().getPosition().x-p.getPosition().x);
					fixture.getBody().setType(BodyType.DYNAMIC);
					fixture.getBody().setLinearVelocity(new Vec2(-1000*(float)Math.cos(angle), -1000*(float)Math.sin(angle)));
				}
				return true;
			}
		}, aabb);
	}

	@Override
	public BonusType getType() {
		return BonusType.SNAP;
	}

	@Override
	public Vec2 getPosition() {
		return new Vec2(x, y);
	}

	@Override
	public Color getColor() {
		return Color.BLACK;
	}

}
